package com.example.presentation_libary;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.example.presentation_libary.eventBus.MsgEvent;
import com.example.presentation_libary.interfaces.ScreenAddViewInterFace;
import com.example.presentation_libary.interfaces.ScreenBindServiceInterFace;
import com.example.presentation_libary.service.MultiScreenService;


/**
 * @author: tjf
 * @date: 2022-10-18
 * @desc： 绑定服务 副屏的添加及使用和释放
 */
public class ScreenServiceUtils {
    public static String TAG = "辅屏服务工具类";
    MultiScreenService multiScreenService;//控制副屏显示的服务
    ServiceConnection serviceConnection;// 代理服务连接的对象


    public ScreenServiceUtils() {

    }

    public static class ScreenServiceUtilsHolder {
        public static final ScreenServiceUtils instance = new ScreenServiceUtils();

    }

    public static ScreenServiceUtils getInstance() {
        return ScreenServiceUtilsHolder.instance;
    }

    public MultiScreenService getMultiScreenService() {
        return multiScreenService;
    }


    //初始化 serviceConnection 对象
    public void initConnection() {
        if (serviceConnection != null) {
            return;
        }
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.i(TAG, "=绑定服务成功 ==");
                MultiScreenService.MultiScreenBinder binder = (MultiScreenService.MultiScreenBinder) service;
                multiScreenService = binder.getService();
                //显示 副屏屏幕
                multiScreenService.showSecondPresentation();
                if (sericeInterFace != null) {
                    sericeInterFace.onServiceConnected();
                }
                if (sericeInterFace != null) {
                    sericeInterFace.showAuxiliaryScreenContent();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                multiScreenService = null;
                Log.i(TAG, "=释放服务成功 ==");
                if (sericeInterFace != null) {
                    sericeInterFace.onServiceDisconnected();
                }
            }
        };
    }

    /**
     * 启动双屏异显的服务
     * 进行为空和没有显示的判断
     */
    public void bindService(Context context) {
        if (sericeInterFace != null) {
            sericeInterFace.initService();
        }
        if (multiScreenService != null && multiScreenService.getPresentation()!=null){
            if (!multiScreenService.getPresentation().isShowing()) {
                multiScreenService.showSecondPresentation();
            }
            if (sericeInterFace != null) {
                sericeInterFace.showAuxiliaryScreenContent();
            }
        } else {
            openMultiScreenPresentation(context);
        }
    }

    //启动服务进行演示辅屏
    private void openMultiScreenPresentation(Context context) {
        Intent intent = new Intent(context, MultiScreenService.class);
        context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    //添加显示的副屏 内容及类型
    public void setAddauxiliary(String ScreenType, String json) {
        if (multiScreenService.getPresentation() != null) {
            multiScreenService.getPresentation().onMsgPresentationBus(new MsgEvent(ScreenType, json));
        }

    }

    // 设置副屏的添加和数据的处理回调
    public void setAddViewInterFace(ScreenAddViewInterFace screenAddViewInterFace) {
        getMultiScreenService().getPresentation().setScreenAddViewInterFace(screenAddViewInterFace);
    }

    //关闭副屏显示
    public void dimssSecondPresentation() {
        if (multiScreenService != null) {
            multiScreenService.dimssSecondPresentation();
        }
    }

    //解绑服务 进行释放
    public void close(Context context) {
        if (serviceConnection != null) {
            context.unbindService(serviceConnection);
            serviceConnection = null;
            setServiceInterFace(null);
        }
    }

    //接口对象
    private ScreenBindServiceInterFace sericeInterFace;

    public void setServiceInterFace(ScreenBindServiceInterFace sericeInterFace) {
        this.sericeInterFace = sericeInterFace;
    }
}
